<script type="text/javascript">
$(function(){    
    $('#btn_submit').click(function(){
        if($('#addid').val()=='' || $('#dizhichange').is(':visible')){
            alert('请保存您的收货地址后再提交国际运单。');
            return false;
        }
        $('#f_waybill').submit();
    });    
    $('#ShowStore').click(function(){
        $('#QDtable').toggle();
        if($(this).text()=='展开查看详情︾'){
            $(this).text('收起查看详情︽');
        }else{
            $(this).text('展开查看详情︾');
        }
        return false;
    });
    $('#aOchange').click(function(){
         
         $('#dizhichange').show();
        $('.dizhi,.Ochange').hide();
        return false;
    });
    //---国家选择器---
    $('.guojiabox').hover(function(){
        //$('.guojiaceng').show();
    },function(){
        $('.guojiaceng').hide();
    });
    $('.guyojiatext').click(function(){
        $('.guojiaceng').show();
    });
    $('.countrylist a').bind('click',function(){
        var city =$(this).text();
        $('#newcountry').val(city);
        
        $('.guojiabox #countryid').val($(this).attr('cid'));
        $('.guojiaceng').hide();
        return false;
    });
    $('.countryzm a').each(function(index,item){
        $(item).click(function(){
            $('.countryzm li.zmon').removeClass('zmon');
            $(this).parent().addClass('zmon');
            $('#countryTab ul').hide();
            $('#countryTab ul').eq(index).show();
            return false;
        });
    });
    
    $('input[name="useaddr"]').live('change',function(){
        if($('#usenew').is(':checked')){
            $('.newdz').show();
        }else{
            $('.newdz').hide();
        }
        $('.changeON').removeClass('changeON');
        $(this).parents('tr').addClass('changeON');
    });
    if($('input[name="useaddr"]').length==1){
        $('#usenew').click();
        $('#usenew').change();
    }
    function validAddress(){
        if($('#newcountry').val()==''){
            alert('请选择国家');
            $('#newcountry').focus();
            return false;
        }
        if($('#newadd').val()==''){
            alert('请输入详细地址');
            $('#newadd').focus();
            return false;
        }
        if($('#newpostcode').val()==''){
            alert('请输入邮编');
            $('#newpostcode').focus();
            return false;
        }
        if($('#newlinkman').val()==''){
            alert('请输入收货人');
            $('#newlinkman').focus();
            return false;
        }
        if($('#newtel').val()==''){
            alert('请输入手机号码');
            $('#newtel').focus();
            return false;
        }
        
        return true;
    }
    $('.baocun').click(function(){
        var posts= {};
        if($('#usenew').is(':checked')){
            if(!validAddress()){
                return false;
            }
            
            $('.changeHX tr').eq(0).before($('#tr_temp').val());
            var newrow = $('.changeHX tr').eq(0);
            $('.newdz :input').each(function(index,item){
                posts[$(item).attr('name')]=$(item).val();
                $('.sel'+$(item).attr('name'),newrow).text($(item).val());
                $('.inp'+$(item).attr('name'),newrow).val($(item).val());
            });
            $(".curcountryText").text($('#newcountry').val());
            $(".inpcountryText").val($('#newcountry').val());            
            $.post('/ajax/addaddress/', posts, function(data){
                $('.inpid',newrow).val(data).attr('id','useaddr_'+data);
                $('#addid').val(data);
                $('label',newrow).attr('for', 'useaddr_'+data);                
            }, 'json');            
            $('.newdz').hide();
        }else{
            $('input',$('input[name="useaddr"]:checked').parents('tr')).each(function(index,item){
                posts[$(item).attr('name')]=$(item).val();
            });
        }
        for(var item in posts){
            $('.cur'+item).text(posts[item]);
        }
        
        $('.kuaidi').append('正在获取运费,请稍等...');
        $.post('/tool/cultfee', {'ids':$('#ids').val(), 'weight':parseInt($('#weight').val()) +parseInt($('#bagWeight').val()),'cid':posts['countryid']}, function(html){
            $('.kuaidi').html(html);
            $('.shiptyperadio:enabled').first().click();
        }, 'html');        
        $('#dizhichange').hide();
        $('.dizhi,.Ochange').show();
        return false;
    });
    
    $('.shiptyperadio').live('change',function(){
        var shipfee = $(this).parents('tr').find('.kd3').text();
        $('#shipfee').text(shipfee);        
        var reportFee = parseInt($(this).attr('data'));
        if(isNaN(reportFee))
            reportFee =0;
        $('#show_reportFee').text(changeTwoDecimal_f(reportFee) );
        cultAmount();
    });
    
    function changeTwoDecimal_f(x)
    {
        var f_x = parseFloat(x);
        if (isNaN(f_x))
        {
        alert('function:changeTwoDecimal->parameter error');
        return false;
        }
        f_x = Math.round(f_x*100)/100;
        var s_x = f_x.toString();
        var pos_decimal = s_x.indexOf('.');
        if (pos_decimal < 0)
        {
        pos_decimal = s_x.length;
        s_x += '.';
        }
        while (s_x.length <= pos_decimal + 2)
        {
        s_x += '0';
        }
        return s_x;
    }
    function cultAmount(){
        var shipfee = $('.shiptyperadio:checked').parents('tr').find('.kd3').text();
       
        var reportFee = parseInt($('#show_reportFee').text());
        var data = $('.couponno:checked').attr('data');
        if(data!=0){
            $.post('/member/validatecoupon', {'couponno':$('.couponno:checked').val(),'money':$('#shipfee').text()}, function(result){
                if(!result.result){
                    alert(result.message);
                }else{
                    $('#coupon_row em').text('-'+changeTwoDecimal_f(result.discount));
                    $('#amount').text('￥'+ changeTwoDecimal_f(parseFloat(shipfee) - parseFloat(result.discount) + reportFee));
                    $('#cou_count').text('1');
                    $('#cou_money').text(changeTwoDecimal_f(result.discount));
                }
            }, 'json');
        }else{
            $('#coupon_row em').text('-'+changeTwoDecimal_f(data));
            $('#amount').text('￥'+ changeTwoDecimal_f(parseFloat(shipfee) - parseFloat(data) + reportFee));
            $('#cou_count').text('0');
            $('#cou_money').text('0.00');
        }
        
    }
    $('.shiptyperadio:enabled').first().click();
    
    $('#coupon_open').click(function(){
        $('#coupon_enter').toggle();
        $('#coupon_row').show();
        return false;
    });
    $('#coupon_close').click(function(){
        $('#coupon_enter').hide();
        return false;
    });
    $('.couponno').change(function(){
        cultAmount();
    });
    
    $('#isNoPackage,#isKong').change(function(){
        if($(this).is(':checked')){
            $(this).parents('td').next().children().show();
        }else{
            $(this).parents('td').next().children().hide();
        }
    });
});
</script>

<div class="center">
	<div class="home">
    	<div class="home_left">
            <?php echo $this->partial('member/menu.phtml',array('uCount' => $this->uCount,'menuId'=>$this->menuId,'user'=>$this->user,'account'=>$this->account)); ?>
        </div>
        <form id="f_waybill" method="POST" name="listForm" action="/member/inwaybill/">        
        <?= $this->formHidden('ids',$this->ids) ?>        
        <?= $this->formHidden('cmd','insert') ?>
    	<div class="home_right">

            <div class="Ostep">
                <ul>
                    <li>我的仓库</li>
                    <li class="os2">填写运输信息</li>
                    <li class="os3">付款完成运单</li>
                </ul>
            </div>
            
            <div class="OrderData">
                <div class="ODleft">
                    运单信息
                </div>
                <div class="ODright">
                    <div class="infobox">
                        <div class="Oinfo">
                            <h1>您本次共提交<span><?php echo $this->storeCount;?></span>个库存物品，物品重量<span><?php echo ($this->weightCount+$this->packWeight);?></span>g</h1>
                            <a id="ShowStore" href="#">展开查看详情︾</a>
                        </div>
                        <div class="QDtable" id="QDtable" style="display:none;">
                            <table>
                                <tr>
                                    <th class="q1">库存号</th>
                                    <th class="q2">名称</th>
                                    <th class="q3">重量</th>
                                    <th class="q4">数量</th>
                                </tr>
                                <?php 
                                    if($this->Stores)
                                    foreach ($this->Stores as $item) {
                                    ?>
                                <tr>
                                    <td class="q1"><?php echo $item->id;?></td>
                                    <td class="q2"><?php echo $item->productName;?></td>
                                    <td class="q3"><?php echo $item->weight;?>g</td>
                                    <td class="q4"><?php echo $item->quantity;?></td>
                                </tr>
                                <?php }?>
                            </table>
                        </div>
                        <div class="Ochange">
                            <a href="/member/mystore/">返回仓库更改运单</a>
                        </div>
                    </div>
                    <div class="baoguo">
                        <input name="weight"  value="<?php echo ($this->weightCount);?>" id="weight"  style="display:none;">
                        <input name="bagWeight"  value="<?php echo ($this->packWeight);?>" id="bagWeight"  style="display:none;">
                        <h2>您的包裹总重量：<em><?php echo ($this->weightCount+$this->packWeight);?></em>g</h2>
                        <span>(物品重量<?php echo $this->weightCount;?>g + 纸箱<?php echo $this->packWeight;?>g)</span>
                        <a href="http://help.uuch.com/?p=216" target="_blank">如何计算纸箱重量？</a>
                    </div>
                    <? 
                    if($this->volumeWeight>$this->weightCount + $this->packWeight){ ?>
                    <div class="baoguo">
                        <h2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;体积重量：<em><?= $this->volumeWeight ?></em>g</h2>
                        <span>(适用于DHL，UPS，Fedex)</span>
                        <a href=" http://help.uuch.com/?p=268" target="_blank">如何计算体积重量？</a>
                    </div>
                    <? } ?>
                </div>
            </div>
            
            <div class="OrderData">
                <div class="ODleft">
                    收货信息 
                </div>
                <?php 
                $defalutItem= $this->defaultAddress ;
                ?>
                <div class="ODright">
                    <?= $this->formHidden('addid',$defalutItem->Id) ?>
                    <div class="dizhi" <?= $defalutItem->Id != NULL ?'':'style="display:none"' ?> >
                        <table>
                            <tr>
                                <td>收 货 人：</td>
                                <td class="curlinkman"><?php echo $defalutItem->linkMan;?></td>
                            </tr>
                            <tr>
                                <td>国　　家：</td>
                                <td class="curcountryText"><?php echo $defalutItem->StateText ?></td>
                            </tr>
                            <tr>
                                <td>收货地址：</td>
                                <td class="curadd"><?php echo $defalutItem->street;?></td>
                            </tr>
                            <tr>
                                <td>邮　　编：</td>
                                <td class="curpostcode"><?php echo $defalutItem->postcode?></td>
                            </tr>
                            <tr>
                                <td>电　　话：</td>
                                <td class="curtel"><?php echo $defalutItem->mobile.' '.$defalutItem->phone ?></td>
                            </tr>
                        </table>
                    </div>
                    <div class="Ochange"  <?= $defalutItem->Id != NULL ?'':'style="display:none"' ?>>
                        <a href="#" id="aOchange">更改收货地址</a>
                    </div>
                    
                    <div  id="dizhichange" class="dizhichange" <?= $defalutItem->Id != NULL?'style="display:none"':'' ?> >
                        <textarea id="tr_temp" style="display:none">
                            <tr class="seladd0 changeON">
                                <td class="dzcz"><input name="useaddr" class="inpid" checked="checked" id="useraddr_0" type="radio" value="${Id}" /> </td>
                                <td><label for="useaddr_0">
                                        <p><span class="seladd">${stree}</span></p>
                                        <p>收货人:<span class="sellinkman">${linkman}</span>,电话：<span class="seltel">${mobile}</span>，邮编：<span class="selpostcode">${postcode}</span></p>
                                        <input type="hidden" class="inpadd" name="add" value="${street}" />
                                        <input type="hidden" class="inppostcode" name="postcode" value="${postcode}" />
                                        <input type="hidden" class="inpcountry" name="country" value="${state}" />
                                        <input type="hidden" class="inpcountryText" name="countryid" value="${state}" />
                                        <input type="hidden" class="inptel" name="tel" value="${mobile}" />
                                        <input type="hidden" class="inplinkman" name="linkman" value="${linkman}" />
                                    </label></td>
                            </tr>
                        </textarea>
                        <table class="changeHX">
                            <?php 
                            $i=1;
                            foreach ($this->adds as $itemAdd) {?>
                            <tr class="seladd<?php echo $itemAdd->id; ?>  <?= $itemAdd->id == $defalutItem->id?'changeON"':'' ?>">
                                <td class="dzcz"><input name="useaddr" <?= $itemAdd->id == $defalutItem->id?'checked="checked"':'' ?>  id="useaddr_<?= $itemAdd->id ?>" type="radio" value="<?php echo $itemAdd->id; ?>" /></td>
                                <td >
                                    <label for="useaddr_<?= $itemAdd->id ?>">
                                        <p><span class="seladd"><?php echo ''.$itemAdd->street.''?></span><span id="state"> <?= $itemAdd->StateText ?></span> </p>
                                    <p><?php echo '收货人：<span class="sellinkman">'.$itemAdd->linkMan.'</span>，电话：<span class="seltel">'.$itemAdd->mobile.'</span> <span class="selpostcode">邮编：'.$itemAdd->postcode."</span>";?>
                                        <?= $this->formHidden('add',$itemAdd->street) ?>
                                        <?= $this->formHidden('postcode',$itemAdd->postcode) ?>
                                        <?= $this->formHidden('countryid',$itemAdd->State) ?>
                                        <?= $this->formHidden('countryText',$itemAdd->StateText) ?>
                                        <?= $this->formHidden('tel',$itemAdd->mobile) ?>
                                        <?= $this->formHidden('linkman',$itemAdd->linkMan) ?>
                                    </p>
                                    </label>
                                </td>
                            </tr>
                            <?php 
                            if($i++>=5){
                                break;
                            }
                            }?>
                            <tr>
                                <td class="dzcz"><input name="useaddr" id="usenew" type="radio" value="true" /></td>
                                <td>
                                    <label for="usenew">使用新地址</label>
                                </td>
                            </tr>
                        </table>
                        <div class="newdz" style="display:none">
                            <table>
                                <tr>
                                    <td class="new_z"><span>*</span>所在国家：</td>
                                    <td>
                                        <div class="guojiabox">
                                            <div class="guyojiatext"><input type="text" readonly="readonly" id="newcountry" name="country" /><a href="javascript:void(0)"></a></div>
                                            <?= $this->formHidden('countryid') ?>
                                            <div class="guojiaceng" style="display:none;">
                                                <div class="remen">
                                                    <h2>热门国家和地区</h2>
                                                    <ul class="countrylist">
                                                        <li><a href="#" id="c_us" cid="1">美国</a></li>
                                                        <li><a href="#" cid="2">加拿大</a></li>
                                                        <li><a href="#" cid="3">澳大利亚</a></li>
                                                        <li><a href="#" cid="47">日本</a></li>
                                                        <li><a href="#" cid="39">台湾</a></li>
                                                        <li><a href="#" cid="4">新西兰</a></li>
                                                        <li><a href="#" cid="5">英国</a></li>
                                                        <li><a href="#" cid="42">马来西亚</a></li>
                                                    </ul>
                                                </div>
                                                <div class="allcountry">
                                                    <h2>全部国家和地区</h2>
                                                    <ul class="countryzm">
                                                        <?
                                                        foreach ($this->words as $char){
                                                        ?>
                                                        <li <?= $char=='A'?'class="zmon"':'' ?>><a href="#"><?= $char ?></a></li>
                                                        <? }
                                                        ?>

                                                    </ul>
                                                    <div id="countryTab">
                                                    <?
                                                    foreach ($this->words as $char){
                                                    ?>
                                                <ul class="countrylist"  <?= $char=='A'?'':'style="display:none"' ?>>
                                                    <?
                                                    $citys= Application_Model_city::GetBySortCode($char);
                                                    foreach ($citys as $city){
                                                    ?>
                                                        <li><a href="#" cid="<?= $city->Id ?>"><?= $city->Name ?></a></li>
                                                        <? } ?>
                                                    </ul>
                                                    <?
                                                        }
                                                    ?>
                                                    </div>
                                                </div>
                                                
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="new_z"><span>*</span>详细地址：</td>
                                    <td><input class="text400 " name="add" maxlength="500" id="newadd" type="text" /></td>
                                </tr>
                                <tr>
                                    <td class="new_z"><span>*</span>邮政编码：</td>
                                    <td><input class="text" name="postcode" maxlength="20" id="newpostcode" type="text" /></td>
                                </tr>
                                <tr>
                                    <td class="new_z"><span>*</span>收货人：</td>
                                    <td><input class="text " name="linkman" maxlength="50" id="newlinkman" type="text" /></td>
                                </tr>
                                <tr>
                                    <td class="new_z"><span>*</span>手机号码：</td>
                                    <td><input class="text " name="tel" id="newtel" maxlength="20" type="text " />
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="changeok">
                            <a class="baocun" href="#"><span>确定</span></a>
                              <a class="quxiao" href="#"><span>取消</span></a>
                        </div>
                    </div>
                </div>
                
            </div>
            
            <div class="OrderData">
                <div class="ODleft">
                    配送方式
                </div>
                <div class="ODright">
                    <div class="kuaidi">
                        <table>
                            <tr>
                                <th class="kd1">方式选择</th>
                                <th class="kd2">按体积<br/>重量计算</th>
                                <th class="kd2">结算重量</th>
                                <th class="kd2">预计天数</th>
                                <th class="kd3">金额(￥)</th>
                            </tr>
                             <?php
                             foreach ($this->prices as $item){
                                 $weight=0;
                                 if($item->Express->IsVolume && $this->volumeWeight>($this->weightCount+$this->packWeight))
                                     $weight = $this->volumeWeight;
                                 else
                                     $weight = $this->weightCount+$this->packWeight;
                                 $item->SortKey = $item->getMoneyByweight($weight);
                             }
                             $prices = Uuch_Util::quickSort($this->prices, 'SortKey');
                             $air = NULL;
                             foreach($prices as $item){   
                                 $accept =true;
                                 $message ='';
                                 foreach ($this->Stores as $si){
                                     $itemAccept =$item->isAccept($si->ItemType);
                                     if(!$itemAccept){
                                         $message .= '不接受'.$si->getItemTypeName();
                                     }
                                     $accept = $accept && $itemAccept;
                                 }
                                 //跳过AIR，在最后面补上
                                 if($item->ExpressId == Application_Model_Express::Express_AIR){
                                     $air = $item;
                                     continue;;
                                 }
                            ?>
                            <tr class="shipRow">
                                <td class="kd1">
                                	<label><input data="<?= $item->Express->ReportFee ?>" type="radio" <?= $accept?'':' disabled="disabled"' ?> class="shiptyperadio" value="<?= $item->ExpressId?>" name="shipType" />
                                        <span><?= $item->getExpress()->Name ?></span>
                                        <span style='color:red'><?= $message ?></span>
                                    </label>
                                </td>
                                <td class="kd2"><?= $item->Express->IsVolume?'√':'×' ?></td>
                                <td class="kd2"><?= $item->Express->IsVolume&& $this->volumeWeight>($this->weightCount+$this->packWeight)? $this->volumeWeight :($this->weightCount + $this->packWeight) ?>g</td>
                                <td class="kd2"><?=  $item->times?>天</td>
                                <td class="kd3"><?= number_format($item->SortKey,2) ?></td>
                            </tr>
                            <?php                                                        
                            }
                            if($air){
                                 $item = $air;
                                 $accept =true;
                                 $message ='';
                                 foreach ($this->Stores as $si){
                                     $itemAccept =$item->isAccept($si->ItemType);
                                     if(!$itemAccept){
                                         $message .= '不接受'.$si->getItemTypeName();
                                     }
                                     $accept = $accept && $itemAccept;
                                 }                                
                            ?>
                            <tr class="shipRow">
                                <td class="kd1">
                                	<label><input data="<?= $item->Express->ReportFee ?>" type="radio" <?= $accept?'':' disabled="disabled"' ?> class="shiptyperadio" value="<?= $item->ExpressId?>" name="shipType" />
                                        <span><?= $item->getExpress()->Name ?></span>
                                        <span style='color:red'><?= $message ?></span>
                                    </label>
                                </td>
                                <td class="kd2"><?= $item->Express->IsVolume?'√':'×' ?></td>
                                <td class="kd2"><?= $item->Express->IsVolume&& $this->volumeWeight>($this->weightCount+$this->packWeight)? $this->volumeWeight :($this->weightCount + $this->packWeight) ?>g</td>                                
                                <td class="kd2"><?=  $item->times?>天</td>
                                <td class="kd3"><?= number_format($item->SortKey,2) ?></td>
                            </tr>                            
                            <? } ?>
                        </table>
                    </div>
                </div>
            </div>
            
            <div class="OrderData">
                <div class="ODleft">
                    收费详情
                </div>  
                <div class="ODright">
                	<div class="feiyong">
                    	<table>
                        	<tr>
                                <td class="fyz">运　　　费：</td>
                                <td><span id="shipfee" style="font-size:14px;color:#000;margin:0"><?= $prices[0]->SortKey?></span>元</td>
                            </tr>
                        	<tr>
                                <td class="fyz">报　关　费：</td>
                                <td ><span id="show_reportFee" style="font-size:14px;color:#000;margin:0">8.00</span>元</td>
                            </tr>
                            <tr id="coupon_row" style="display:none">
                                <td class="fyz">优　惠　券：</td>
                                <td><em>-0.00</em>元</td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            
            <div class="OrderData">
                <div class="ODleft">
                    包装操作
                </div>
                <div class="ODright">
                	<div class="feiyong">
                    	<table>
                        	<tr>
                                <td class="fyz"><label><input id="isNoPackage" name="isNoSkin" type="checkbox" value="1" />去掉物品非必要包装来节省运费 </label></td>
                                <td><div style="color:#ccc; display:none"> 您选择了去掉非必要包装，此项节省的钱将返回您的帐户</div></td>
                            </tr>
                        	<tr>
                                <td class="fyz"><label><input id="isKong" name="isCompress" type="checkbox" value="1" />进行真空处理，缩小体积重量节省运费 </label></td>
                            	<td><div style="color:#ccc;display:none">您选择了真空处理，此项节省的钱将返回您的帐户</div></td>
                            </tr> 
                        </table>
                    </div>
                </div>
            </div>            
            <div class="accounts_num">
                <table>
                    <tr>
                        <td class="preferential">
                            <ul>
                                <li>
                                    <h3><a href="#" id="coupon_open">(+)使用优惠卷抵消部分总额</a></h3>
                                    <div id="coupon_enter" class="offset" style="display:none;">
                                        <div class="offset_t">
                                            <h4>提示：优惠卷每次只能使用1张！</h4><a id="coupon_close" href="#">关闭</a>
                                        </div>
                                        <div class="offset_z">
                                            <h4>请选择要使用的优惠券：</h4>
                                            <dl>
                                                <dd><label><input class="couponno" name="couponno" data="0" checked="checked" type="radio" value="0" />[不使用优惠券]</label></dd>
                                                <? foreach($this->coupons as $coupon){ ?>
                                                <dd><label><input class="couponno" data="<?= $coupon->getDiscountMoney() ?>" name="couponno" type="radio" value="<?= $coupon->No ?>" /><?= $coupon->getTitle() ?></label></dd>
                                                <? } ?>
                                            </dl>
                                        </div>
                                        <div class="huode">
                                            <span>您使用了 <em id="cou_count">0</em> 张优惠券　可以优惠 <em id="cou_money">0.00</em> 元</span>
                                        </div>
                                    </div>
                                </li>
                            </ul>                           
                        </td>
                        <td class="total"><span>应付总额：</span><em id="amount">￥0.00</em><span>元</span></td>
                    </tr>
                </table>
            </div>
            <div class="Order_ok">
            	<dl>
                	<dt>
                    	<div class="Qbz">最终包裹重量以实际打包为准，多余的钱将返还至您的帐户</div>
                    </dt>
                    <dt>
                        
                    </dt>
                    <dd><input type="button" id="btn_submit" value="" onmouseover="this.className='Order_After'" onmouseout="this.className=''" /></dd>
                </dl>            	
            </div>            
        </div>        
        </form>
	</div>
</div>
